<?php
include_once("../../php/include_all.php");
?>
<div class="widget-header widget-header-flat ">
    <h5><i class="icon-calendar"></i>Bidang Bappeda</h5>
</div>
<div class="position-relative" id="page-content">
    <a href="#modalwin" data-toggle="modal" class="btn btn-small btn-primary2 no-radius btn-tambah"><i class="icon-plus"></i>&nbsp;Tambah Data</a>
    &nbsp;<span id="load" class="spinner"></span>
</div>
<div id="modalwin" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

    <header class="modal-header">
        <a href="#" class="close" data-dismiss="modal">x</a>
        <h3>Data Bidang</h3>
    </header>

    <div class="modal-body">
        <form id="form_bidang" class="form-horizontal" >
            <input type="hidden" name="act" value="save_bap">
            <input type="hidden" id="id_bdgbapeda" name="id_bdgbapeda" value="0">
            <table class="table-form">
                <tbody>
                <div class="control-group">
                    <label class="control-label" for="des">Nama Bidang :</label>
                    <div class="controls">  
                        <input type="text" id="nama_bdgbapeda"  laceholder="nama Bidang bappeda" name="nama_bdgbapeda" >
                        <a class="btn btn-primary2 btn-small bt-simpan-bap"><div id="loader" style="margin-top:2px"></div><i class="icon-save"></i>&nbsp;Simpan</a><span id="result"></span>
                        <span id="result"></span>
                    </div>
                </div>
                </tbody>
            </table>
        </form>
    </div>
</div>
<div class="box-content">
    <table class="table table-bordered geo-table">
        <thead>
            <tr>
                <th class="center" width="2%">No</th>
                <th class="center" width="60%">Nama Bidang</th>
                <th class="center" width="38%">Aksi</th>
            </tr>
        </thead>
        <tbody id="tbody_bap">
            <?php
            $x = 1;
            $bidang = get_datas("select * from mus_bidang_bappeda");
            foreach ($bidang as $bidang) {
                ?>
                <tr>
                    <td class="center"><?php echo $x ?></td>
                    <td><?php echo $bidang['nama_bdgbapeda']; ?></td>
                    <td class="center">
                        <a  href="#modalwin" data-toggle="modal"  class="btn btn-info btn-small bt-edit-bap" name="<?php echo $bidang['id_bdgbapeda']; ?>"><i class="icon-edit icon-white"></i> ubah</a>
                        <a class="btn btn-danger btn-small bt-delete-bap" name="<?php echo $bidang['id_bdgbapeda']; ?>"><i class="icon-trash icon-white"></i> hapus</a>

                    </td>
                </tr>
                <?php
                $x++;
            }
            ?>
        </tbody>
    </table>
</div>

<script>
    var urls = 'admin/master_bappeda/bappeda_save.php';

    $('.bt-simpan-bap').click(function(){
        var btn  = $(this);
        var load = $('#loader');
        var rslt = $('#result');
        load.addClass('icon-spinner icon-spin icon-2x white');
       
	
        var form = $('#form_bidang');
        var data = form.serializeArray();
        var post = $.post(urls,data);
        post.done(function(res){
            var result = res.split('__');
            //   0      1     2
            //success__iddkecamcatan__(isi tbody)tr
            if(result.length==3){
                if(result[0]=='success'){
                    rslt.html('<font color="green">tersimpan...</font>');
                    setTimeout(function(){
                        rslt.html('');
                    },1500);
				
                    var tbody = $('#tbody_bap');
                    tbody.html(result[2]);
					init();
					$('#id_bdgbapeda').val('0');
                    $('#nama_bdgbapeda').val(' ');
					$('#modalwin').modal('hide');
                  
                }else{
                    rslt.html('<font color="red">'+res+'</font>');
                }
            }else{
                rslt.html('<font color="red">'+res+'</font>');
            }
            load.removeClass();
            
        });
    });
	
    $('.btn-tambah').click(function(){ 
		$('#id_bdgbapeda').val('0');
        $('#nama_bdgbapeda').val(' ');              
    });
	
    function init(){
        $('.bt-delete-bap').click(function(){
            var btn  = $(this);
            var pid  = this.name;
            var load = $(this);
            load.html('<div class="icon-spinner icon-spin icon-2x white"></div>');
            bootbox.confirm("Anda akan menghapus bidang ini?", function(result) {
                if(result==true){
                    if(pid!=0){
                        var post = $.post(urls,{act:"del_bap",id_bdgbapeda:pid});
                        post.done(function(data){
						
						var hasil = data.split('__');
							if(hasil.length==2){
								if(hasil[0]='success'){
									//btn.parent().parent().remove();
									var tbody = $('#tbody_bap');
									tbody.html(hasil[1]);
									init();
								}
							}
                        });
                        load.html('<i class="icon-trash icon-white"></i> hapus');
                    }
                }else{
                    load.html('<i class="icon-trash icon-white"></i> hapus');
                }
            });
        });
        
        $('.bt-edit-bap').click(function(){
            var id_bdgbapeda= this.name;
            var form = $('#form_bidang');
            var post = $.post(urls,{act:'edit_bap',id_bdgbapeda:id_bdgbapeda});
            post.done(function(res){
                var value = res.split('__');  
                //alert(res);
                $('#id_bdgbapeda').val(value[0]);
                form.find('input[name="nama_bdgbapeda"]').val(value[1]);
		
				//document.getElementById('form_bidang').reset();
            });
        });
    }
/*	
$('.close').click(function() {
	$('#id_bdgbapeda').val(0);
	document.getElementById('form_bidang').reset();
});*/
    init();
</script>